﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;

using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;

namespace SIPO.Web.Forms.Transaction
{
   public partial class recInvoiceInfoform : BasePage
    {
        private string id = string.Empty;

        recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

        protected override void OnLoad(EventArgs e)
        {
            
            if (Request.Params["id"] != null)
            {
                id = Request.Params["id"];
            }

            //txtInvoiceDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            if (!Page.IsPostBack)
            {
                if (id.Trim().Length > 0) // Edit an existing item
                {
                    recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                    recInvoice obj = recInvoiceDal.GetById(id.Trim());
                    lblInvoiceDate.Text = obj.InvoiceDate.ToString("dd/MM/yyyy");
                    lblInvoiceNo.Text = obj.InvoiceNo.Trim();
                    hfID.Value = obj.InvoiceNo.Trim();

                    lblTotalTagihan.Text = Convert.ToDecimal(obj.InvoiceValue).ToString("N2");

                    //lblTotalTagihan.Text = Helper.ToDecimal(obj.InvoiceValue.ToString(), NumberStyles.Currency, Helper.Culture.English).ToString();// sanction.Value.ToString();
                    if (obj.DebtorSectorID != null && obj.DebtorSectorID != 0)
                    {
                        recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                        recDebtorSector objDebtorSector = recDebtorSectorDal.GetById(obj.DebtorSectorID);

                        if (objDebtorSector != null)
                        {
                            if (objDebtorSector.SectorID != 0)
                            {
                                recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recSector objSector = recSectorDal.GetById(objDebtorSector.SectorID);
                                if (objSector != null)
                                {
                                    lblSector.Text = objSector.SectorCode + " - " + objSector.SectorName;
                                }
                            }


                            if (objDebtorSector.SubSectorID != 0)
                            {
                                recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recSubSector objSubSector = recSubSectorDal.GetById(objDebtorSector.SubSectorID);
                                if (objSubSector != null)
                                {
                                    lblSubSektor.Text = objSubSector.SubSectorCode + " - " + objSubSector.SubSectorName;
                                }
                            }


                            if (objDebtorSector.SubSubSectorID != 0)
                            {
                                recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recSubSubSector objSubSubSector = recSubSubSectorDal.GetById(objDebtorSector.SubSubSectorID);
                                if (objSubSubSector != null)
                                {
                                    lblSubSubSector.Text = objSubSubSector.SubSubSectorCode + " - " + objSubSubSector.SubSubSectorName;
                                }
                            }


                            
                        }
                    }
                }

                //GetPrivilege();

            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    
    }
}
